/**
 * 整车平台---修改设计的JS文件
 */

$(document).ready(function(){
	
	nowColorControl();//车身颜色选择
 
});

function getCarList(cartype){
      $("#loading").showLoading(); 
	  $.ajax({  
	        type : "POST",  
	        url : CONTEXT_PATH+'front/carDesign/getCarList?cartype='+cartype,  
	        dataType : "json",  
	        cache : false,  
	        success : function(data) {  
                $(".left-oneImgList.listImgBox").empty();
                
                for(i=0;i<data.length;i++){
                    $(".left-oneImgList.listImgBox").append(
        					"<div class='left-oneImgBlock leftImgBlock "+(data.id==$("#carid").val()? 'activeImg':'')+"'>"+
        					"<div class='left-oneImgBox'><img src='"+FILESERVER_PATH+"?id="+data[i].carsidepic+"'/></div>"+
        					"<div class='left-oneImgName'>"+data[i].name+"</div>"+
        				        "<input type='hidden' value="+JSON.stringify(data[i])+">"+
        				    "</div>");
                }
                imgSelectControl();
                $('#loading').hideLoading();
                
	        }  
	    }); 
	
}

function nowColorControl(){
	var $body = $(document.body);
	
	$(".rightForm-carColor").on("click",function(){
		var thisItem=$(this);
		
		$("#carcorlor").val(thisItem.find("input").val());
		
		if(!(thisItem.hasClass("nowColor"))){
			thisItem.addClass("nowColor");
			thisItem.siblings().removeClass("nowColor");
		}
	});
	
	$("#carSideBlack").click(function(){
		 $body.attr('class','client-body clm-body blackActive');
	});
	$("#carSideWhite").click(function(){
		 $body.attr('class','client-body clm-body whiteActive');
	});
	$("#carSideRed").click(function(){
		 $body.attr('class','client-body clm-body redActive');
	});
	$("#carSideBlue").click(function(){
		 $body.attr('class','client-body clm-body blueActive');
	});
}

 
//type==1   留在原页面 
//type==2   返回上一级页面 
//type==3   新建界面点击完成并退出
function submitCarDesignform(carid,type){

	  if(!$.trim($("#designname").val())){
		   jAlert("设计名不能为空","提示");
		   return;
	  }
	
	   
	   if($("#carid").val()==""){
		   jAlert("未选中车型","提示");
		   return;
	   }
	
	  
	   //检查设计名是否重复
	   $.ajax({  
	        type : "POST",  
	        url : CONTEXT_PATH+'common/checkNameUnique',  
	        dataType : "json",  
	        cache : false,  
	 	    data:{id:$("#id").val(),propertyvalue:$("#designname").val(),category:'CarDesignName',where:' and del=0 and userid='+userid},
	        success : function(data) {
	          
              if(data.result==true){
         		   jAlert(data.msg,"提示");
                   return;
              }else{
      
            	   //第一次选中车型或者更改之前的车型
	           	   if($("#carid").val()!=carid){
	           		   
	           		    checkMemory($("#carid").val(),type);
	        	   
	           	   }else{
	           		    submitCarDesignData(carid,type);
	           		
	        	   }
            	  
	           }

	        }  
       }); 
 
}

function checkMemory(carid,type){
	
	   //检查可用容量
	   $.ajax({  
	        type : "POST",  
	        url : CONTEXT_PATH+'front/carDesign/checkMemory',  
	        dataType : "json",  
	        cache : false,  
	 	    data:{carid:carid,isupdate:false},
	        success : function(data) {
		          
	           if(data.result==false){
	      		    jAlert(data.msg,"提示");
	                return;
	           }else{
	
	        	    submitCarDesignData(carid,type);
		       }

	        }  
}); 
}


function submitCarDesignData(carid,type){
	
	   // 新建界面点击完成且未选中车型的情况
	   if(type=='3'&&$("#carid").val()==""){
	       window.location.href= CONTEXT_PATH+'front/carDesign/carDesign_list';
	       return;
	   }


	  var  url;
	  //未保存过车型
	  if(carid==""){
		  url=CONTEXT_PATH+'front/carDesign/updateCarDesign2';
	  }else{
		  //保存过车型
		  url=CONTEXT_PATH+'front/carDesign/updateCarDesign3';
	  }
	 
	   $("#loading").showLoading(); 
 
	   $('#carDesignform').ajaxSubmit({
		    dataType: "json",  
		    url: url,
		    type : "POST",  
		    success: function(data) {  
		    	 $('#loading').hideLoading();
             if(data.result){
               
             	 if(type=='1'){
             		 $("#carModifyModal").modal('show');  
             	 }else{
             		 window.location.href= CONTEXT_PATH+'front/carDesign/carDesign_list';
             	 }
             
             	
             } 
		    }   
		  
	  });  
}



function submitCarLogoModify(){
	
	
	  if(!$.trim($("#designname").val())){
		   jAlert("设计名不能为空","提示");
		   return;
	  }
	   //检查设计名是否重复
	   $.ajax({  
	        type : "POST",  
	        url : CONTEXT_PATH+'common/checkNameUnique',  
	        dataType : "json",  
	        cache : false,  
	 	    data:{id:$("#id").val(),propertyvalue:$("#designname").val(),category:'CarDesignName',where:' and del=0 and userid='+userid},
	        success : function(data) {
		          
	           if(data.result==true){
	      		    jAlert(data.msg,"提示");
	                return;
	           }else{
	        	   submitCarLogoModifyData();
		       }

	        }  
       }); 
	
}


function submitCarLogoModifyData(){
	
	editor.sync('content');
    $("#loading").showLoading(); 
 
	$('#carLogoform').ajaxSubmit({
	    dataType: "json",  
	    url:   CONTEXT_PATH+"front/carDesign/updateCarDesign1",
	    type : "POST",  
	    success: function(data) {  
	    	$('#loading').hideLoading();
            if(data.result){
         	          
     		   $('#carModifyModal').on('hidden.bs.modal', function () {
                   window.location.reload();
   		       });
     		   $("#carModifyModal").modal('show');  
            }

	    }   
	});  
 
}

function download(designid,carid){
	
	    if(!carid){
			   jAlert("未选中车型","提示");
			   return  
		}
	    $("#designid").val(designid);
		$("input[name='chose']").prop("checked",false);
		$("#loadModal").modal('show');
	 
}

function carDesignListToURL(type,designid,carid){
	
	
     if(type==1){
    	 window.location.href=CONTEXT_PATH+'front/carDesign/carDesignModify?id='+designid;
     }else if(type==2){
    
    	if(!carid){
 		   jAlert("未选中车型","提示");
 		   return  
 	    }
    	window.location.href=CONTEXT_PATH+'front/carDesign/partList?designid='+designid;
     }else if(type==3){
    	
    	if(!carid){
 		   jAlert("未选中车型","提示");
 		   return  
     	}
    	window.location.href=CONTEXT_PATH+'front/carDesign/craftOrder?designid='+designid;
     }
	
	
}

function submitDownload(){
	
	if ($("input[name='chose']:checked").length!=0) {
		$("#downloadForm").submit();
		$("#loadModal").modal('hide');
	}else{
		jAlert("请先至少选中一个","提示");
	}
 
}


function delCarDesign(designid){
	
	  jConfirm('确定要删除吗?', '操作确认', function(r) {
    	  
 	     if(r){
		      $("#loading").showLoading(); 
			  $.ajax({  
			        type : "POST",  
			        url : CONTEXT_PATH+'front/carDesign/delCarDesign?id='+designid,  
			        dataType : "json",  
			        cache : false,  
			        async : false,  
			        success : function(data) {  
			        	$('#loading').hideLoading();
			        	if(data.result){
			        		 jAlert("删除成功","提示", function(r) {
			        			  window.location.reload();
			        		 });
			        	
			        	}else{
			        		 $('#loading').hideLoading();
			        		 jAlert("删除时出错","提示");
			        	}
			        	
			        }  
			    }); 
 	      }
	  });
}
